package com.tv.enoguia.adapter;

import java.util.ArrayList;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.tv.enoguia.R;
import com.tv.enoguia.bean.Bussiness;
import com.tv.enoguia.bean.Categories;

import android.content.Context;
import android.graphics.Bitmap.Config;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListBussinessAdapter extends ArrayAdapter<Bussiness> {

	Context context;
	ArrayList<Bussiness> bussinesses = new ArrayList<Bussiness>();
	
	DisplayImageOptions options = new DisplayImageOptions.Builder()
									.cacheInMemory(false).cacheOnDisc(true)
									.bitmapConfig(Config.RGB_565)
									.displayer(new FadeInBitmapDisplayer(300))
									.build();

	public ListBussinessAdapter(Context context,
			ArrayList<Bussiness> bussinesses) {
		super(context, R.layout.item_list_bussiness, bussinesses);
		this.context = context;
		this.bussinesses = bussinesses;
	}

	@Override
	public int getCount() {
		return bussinesses.size();
	}

	@Override
	public Bussiness getItem(int position) {
		return bussinesses.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View v, ViewGroup parent) {
		
		Holder holder = null;
		if (v==null){
			LayoutInflater inflater = LayoutInflater.from(getContext());
			v = inflater.inflate(R.layout.item_list_bussiness, parent, false);
			holder = new Holder();

			
			holder.txtName = (TextView) v.findViewById(R.id.txt_name);
			holder.txtNameBussiness = (TextView) v.findViewById(R.id.txt_name_bussiness);
			holder.imgBussiness = (ImageView) v.findViewById(R.id.img_bus);
			
			v.setTag(holder);
		} else {
			holder = (Holder) v.getTag();
		}
		
		holder.txtName.setText(bussinesses.get(position).getTownName());
		String name = bussinesses.get(position).getCellarName();
		if (name.length() > 30){
			holder.txtNameBussiness.setText(name.substring(0, 27)+"...");
		} else {
			holder.txtNameBussiness.setText(name);
		}
		
		
		ImageLoader.getInstance().displayImage(bussinesses.get(position).getUrlBussiness(), holder.imgBussiness, options);
		
		return v;
	}
	
	class Holder{
		TextView txtName, txtNameBussiness;
		ImageView imgBussiness;
	}
	
	
	

}
